<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜单管理 iframe 框</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
     style="padding: 20px 30px 0 0;">
    <input type="hidden" id="menuId"/>
    <div class="layui-form-item">
        <label class="layui-form-label">父级菜单</label>
        <div class="layui-input-inline">
            <select id="menuParentId" lay-verify="required">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单名称</label>
        <div class="layui-input-inline">
            <input type="text" id="menuName" lay-verify="required" placeholder="请输入菜单名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">路径</label>
        <div class="layui-input-inline">
            <input type="text" id="menuUrl" lay-verify="required" placeholder="请输入菜单路径" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图标路径</label>
        <div class="layui-input-inline">
            <input type="text" id="menuIcoUrl" lay-verify="required" placeholder="请输入图标路径" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="menu-submit" id="menu-submit"
               value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit"
               value="确认编辑">
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script src="../../js/jquery-1.8.3.min.js"></script>
<script>
    //下拉框初始化
    $.ajax({
        url: '/menu/toAdd',
        type: 'get',
        dataType: 'json',
        data: {},
        async: false,//设置阻断
        success: function (data) {
            if (data.status != 200) {
                layer.msg('查询失败！');
            } else {
                //将下拉框中添加数据
                var modelList = data.data;
                if (modelList && modelList.length != 0) {
                    var def = "<option value='0'>顶级菜单</option>";
                    $("#menuParentId").append(def);
                    for (var i = 0; i < modelList.length; i++) {
                        var option = "<option value=\"" + modelList[i].id + "\"";
                        option += ">" + modelList[i].name + "</option>";  //动态添加数据
                        $('#menuParentId').append(option);
                    }
                }
            }
        }
    });

    //根据父级页面判断，如果是修改，则添加默认值
    if (parent.menuType == "toUpdate") {
        $.ajax({
            url: '/menu/findOne',
            type: 'post',
            dataType: 'json',
            async: false,//设置阻断
            data: {"id": parent.menuId},
            success: function (data) {
                $("#menuId").val(data.data.id);
                $("#menuName").val(data.data.name);
                $("#menuName").val(data.data.name);
                $("#menuUrl").val(data.data.url);
                $("#menuIcoUrl").val(data.data.icon);
                //选中下拉框中的option
                $("#menuParentId").find("option[value=" + data.data.parentid + "]").prop("selected", true);
            }
        });
    }

    //layui配置
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
            var $ = layui.$
                , form = layui.form;
            //监听提交
            form.on('submit(menu-submit)', function (data) {
                var field = data.field; //获取提交的字段
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                //获取id，若无则改为0
                var id = $("#menuId").val();
                if (id == null || id == "") id = 0;
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.ajax({
                    url: parent.menuURL,
                    type: 'post',
                    dataType: 'json',
                    async: false,//设置阻断
                    data: {
                        "id": id, "parentid": $("#menuParentId").val(), "name": $("#menuName").val(),
                        "url": $("#menuUrl").val(), "icon": $("#menuIcoUrl").val()
                    },
                    success: function (data) {
                        parent.layer.msg(data.msg, {icon: data.icon, zIndex: layer.zIndex + 100});
                    }
                });
                parent.layui.table.reload('menuTable'); //重载表格
                parent.layer.close(index); //再执行关闭
            });
        }
    )
</script>
</body>
</html>